package com.deng.leaf.core.segment.dao.impl;

import com.deng.leaf.core.segment.dao.IDAllocDao;
import com.deng.leaf.core.segment.mapper.IDAllocMapper;
import com.deng.leaf.core.segment.model.LeafAlloc;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class IDAllocDaoImpl implements IDAllocDao {

    @Resource
    private IDAllocMapper allocMapper;


    @Override
    public List<LeafAlloc> getAllLeafAllocs() {
        return allocMapper.getAllLeafAllocs();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LeafAlloc updateMaxIdAndGetLeafAlloc(String tag) {
        int count = allocMapper.updateMaxId(tag);
        if(count <= 0){
            throw new RuntimeException("更新失败");
        }

        return allocMapper.getLeafAlloc(tag);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LeafAlloc updateMaxIdByCustomStepAndGetLeafAlloc(LeafAlloc leafAlloc) {
        int count = allocMapper.updateMaxIdByCustomStep(leafAlloc);
        if(count <= 0){
            throw new RuntimeException("更新失败");
        }

        return allocMapper.getLeafAlloc(leafAlloc.getKey());
    }

    @Override
    public LeafAlloc getLeafByTag(String tag) {
        return allocMapper.getLeafByTag(tag);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LeafAlloc updateMaxIdEqInitValueByTag(String tag) {
        int count = allocMapper.updateMaxIdEqInitValueByTag(tag);
        if(count <= 0){
            throw new RuntimeException("更新失败");
        }

        return allocMapper.getLeafByTag(tag);
    }

    @Override
    public List<String> getAllTags() {
        return allocMapper.getAllTags();
    }
}
